package com.woniuxy.services.impl;

import com.woniuxy.daos.TbUserDao;
import com.woniuxy.daos.TbUserRoleDao;
import com.woniuxy.daos.TbWorkerDao;
import com.woniuxy.entitys.TbUser;
import com.woniuxy.entitys.TbUserExample;
import com.woniuxy.entitys.TbUserRole;
import com.woniuxy.entitys.TbWorker;
import com.woniuxy.services.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Classname UserServiceImpl
 * @Date 2024/1/14 16:03
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private TbUserDao userDao;
    @Autowired
    private TbWorkerDao workerDao;
    @Autowired
    TbUserRoleDao userRoleDao;
    //添加工作人员
    @Override
    public int addUser(String phone,Integer rid) {
        List<TbUser> tbUsers = userDao.selectByExample(new TbUserExample());
        boolean tag = true;
        for(TbUser users : tbUsers){
            if (users.getPhone().contains(phone)) {
                tag = false;
                break;
            }
        }
        TbUser user = new TbUser();
        user.setPhone(phone);
        user.setUpwd("123456");
        int i = userDao.insert(user);
        if(i>0){
            TbWorker tbWorker = new TbWorker();
            tbWorker.setUid(user.getUid());
            tbWorker.setSoftdel(1);
            workerDao.insert(tbWorker);

            TbUserRole userRole = new TbUserRole();
            userRole.setRid(rid);
            userRole.setUid(user.getUid());
            userRoleDao.insert(userRole);
        }
        return i;
    }
}
